package com.tianpengtech.common.upload;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

public class QiNiuUpload {

	private final static String accessKey = "b0PyM0GiK3GP2c4Udr519XirHYk9sYlacG4499rt";
	private final static String secretKey = "dizHTFt75PGbL8UV0DBraQrei7ex-lw8-wvuQRsA";
	private final static String bucket = "tianpengtech";

	/**
	 * 上传七牛云存储服务器
	 * @param localFile
	 * @return
	 */
	public static String upload(String localFile) {
		Configuration cfg = new Configuration(Zone.autoZone());
		UploadManager uploadManager = new UploadManager(cfg);
		String key = null;
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		String fileName="";
		try {
			Response response = uploadManager.put(localFile, key, upToken);
			DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			fileName = putRet.hash;
		} catch (QiniuException ex) {
			
		}
		
		return fileName;
	}

}
